package com.info.zhiduoduo.admin.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.mapper.Condition;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.marketingcooperative.CompanySwitchMarketingCooperativeFormBean;
import com.info.zhiduoduo.admin.model.formbean.marketingcooperative.ListMarketingCooperativePosterConfigFormBean;
import com.info.zhiduoduo.admin.model.formbean.partner.PartnerBindMarketingCooperativeFormBean;
import com.info.zhiduoduo.common.configs.UploadPathConfig;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.FileUtil;
import com.info.zhiduoduo.repository.dto.marketingcooperative.MarketingCooperativePosterConfigDto;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.marketingcooperative.MarketingCooperative;
import com.info.zhiduoduo.repository.entity.marketingcooperative.MarketingCooperativeCompanyForm;
import com.info.zhiduoduo.repository.entity.marketingcooperative.MarketingCooperativeCompanyFormItem;
import com.info.zhiduoduo.repository.entity.marketingcooperative.MarketingCooperativePosterConfig;
import com.info.zhiduoduo.repository.mapper.company.CompanyAccountMapper;
import com.info.zhiduoduo.repository.mapper.marketingcooperative.MarketingCooperativeMapper;
import com.info.zhiduoduo.repository.mapper.marketingcooperative.MarketingCooperativePosterConfigMapper;
import com.info.zhiduoduo.repository.mapper.marketingcooperative.MarketingCooperativeRecordMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class MarketingCooperativeService {

	@Resource
	MarketingCooperativeMapper marketingCooperativeMapper;
	@Resource
	MarketingCooperativePosterConfigMapper marketingCooperativePosterConfigMapper;
	@Resource
	MarketingCooperativeRecordMapper marketingCooperativeRecordMapper;
	@Resource
	CompanyAccountMapper companyAccountMapper;

	@Autowired
	UploadPathConfig uploadPathConfig;

	public SmartResult companySwitchMarketingCooperative(BaseSimpleFormBean<CompanySwitchMarketingCooperativeFormBean> formBean) {
		CompanySwitchMarketingCooperativeFormBean reqdata = formBean.getReqdata();
		String compids = reqdata.getCompids();
		int state = reqdata.getState();
		companyAccountMapper.switchMarketingCooperative(compids, state);
		//开通企业时默认生成表单
		if (state == 1){
			String[] compId = compids.split(",");
			for(String id : compId){
				Company company = new Company().selectById(Integer.valueOf(id));
				if(company != null) {
					MarketingCooperativeCompanyForm whether = new MarketingCooperativeCompanyForm()
							.selectOne(Condition.create().eq("compid", company.getCompid()));
					if (whether == null) {
						MarketingCooperativeCompanyForm Form = new MarketingCooperativeCompanyForm();
						Form.setCompid(company.getCompid()).setName("营销");
						if (Form.insert()) {
							MarketingCooperativeCompanyFormItem item = new MarketingCooperativeCompanyFormItem();
							item.setCompid(Form.getCompid())
									.setFormId(Form.getId())
									.setType(1)
									.setColumnName("col1")
									.setName("联系人");
							if (item.insert()) {
								MarketingCooperativeCompanyFormItem item1 = new MarketingCooperativeCompanyFormItem();
								item1.setCompid(Form.getCompid())
										.setFormId(Form.getId())
										.setType(1)
										.setColumnName("col2")
										.setName("联系方式");
								if (item1.insert()) {
									MarketingCooperativeCompanyFormItem item2 = new MarketingCooperativeCompanyFormItem();
									item2.setCompid(Form.getCompid())
											.setFormId(Form.getId())
											.setType(2)
											.setColumnName("col3")
											.setName("业务需求").insert();
								}
							}
						}
					}
				}
			}

		}
		return SmartResult.success();
	}

	public SmartResult partnerBindMarketingCooperative(BaseSimpleFormBean<PartnerBindMarketingCooperativeFormBean> formBean) {
		PartnerBindMarketingCooperativeFormBean reqdata = formBean.getReqdata();
		List<Integer> partnerids = reqdata.getPartnerids();
		int compid = reqdata.getCompid();
		int state = reqdata.getState();
		for (Integer partnerid : partnerids) {
			MarketingCooperative entity = marketingCooperativeMapper.getByCompidAndPartnerid(compid, partnerid);
			if (entity == null) {
				// Create new one
				entity = new MarketingCooperative().setCompid(compid).setPartnerid(partnerid).setState(state);
			} else {
				entity.setState(state);
			}
			entity.insertOrUpdate();
		}
		return SmartResult.success();
	}

	public SmartResult saveMarketingCooperativePosterConfig(BaseSimpleFormBean<MarketingCooperativePosterConfig> formBean) {
		MarketingCooperativePosterConfig reqdata = formBean.getReqdata();
		reqdata.insert();
		return SmartResult.success(reqdata);
	}

	public SmartResult editMarketingCooperativePosterConfig(BaseSimpleFormBean<MarketingCooperativePosterConfig> formBean) {
		MarketingCooperativePosterConfig reqdata = formBean.getReqdata();
		Integer id = reqdata.getId();
		MarketingCooperativePosterConfig poster = marketingCooperativePosterConfigMapper.selectById(id);
		if (poster != null) {
			// Delete the old poster picture.
			String posterPath = poster.getPosterPath();
			FileUtil.deleteFile(new File(uploadPathConfig.getUploadpath(), posterPath));
		}
		reqdata.updateById();
		return SmartResult.success(reqdata);
	}

	public SmartResult<MarketingCooperativePosterConfigDto> listPosterConfigs(
			BaseSimpleFormBean<ListMarketingCooperativePosterConfigFormBean> formBean) {
		ListMarketingCooperativePosterConfigFormBean reqdata = formBean.getReqdata();

		Page<MarketingCooperativePosterConfigDto> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
		marketingCooperativePosterConfigMapper.listByName(reqdata.getConfigName(), reqdata.getCompname());
		return SmartResult.ofPage(page);
	}
}
